package com.example.taximeter.service.impl;

import com.example.taximeter.bean.Fact;
import com.example.taximeter.bean.Price;
import com.example.taximeter.bean.ReturnInfo;
import com.example.taximeter.mapper.ParamFileMapper;
import com.example.taximeter.mapper.PriceMapper;
import com.example.taximeter.mapper.TaximeterMapper;
import com.example.taximeter.service.PriceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 价格类别
 */
@Service
public class PriceServiceImpl implements PriceService {

    private PriceMapper priceMapper;
    private TaximeterMapper taximeterMapper;
    private ParamFileMapper paramFileMapper;

    @Autowired
    public PriceServiceImpl(PriceMapper priceMapper,TaximeterMapper taximeterMapper,ParamFileMapper paramFileMapper){
        this.priceMapper = priceMapper;
        this.taximeterMapper = taximeterMapper;
        this.paramFileMapper = paramFileMapper;
    }

    @Override
    public List<Price> findPriceList() {

        return priceMapper.findPriceList();
    }

    @Override
    public String findPricetypeByPriceid(Integer id) {
        return priceMapper.findPricetypeByPriceid(id);
    }

    @Override
    public ReturnInfo findPriceAll(Integer pageNum, Integer pageSize, Price price) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<Price> prices = priceMapper.findPriceAll(price);
        PageInfo<Price> pageInfo = new PageInfo<>(prices);
        if(prices.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public String addPrice(Price price) {
        if(priceMapper.findPriceByPriceType(price.getPricetype()).size()!=0){
            return "价格类型重复";
        }else if(priceMapper.addPrice(price)){
            return "成功";
        }else {
            return "失败";
        }

    }

    @Override
    public String updatePrice(Price price) {
        List<Price> prices = priceMapper.findPriceByPriceType(price.getPricetype());
        if(prices.size()==1){
            if(prices.get(0).getId()!=price.getId()){
                return "价格类型重复";
            }else {
                if(priceMapper.updatePrice(price)){
                    return "成功";
                }else {
                    return "失败";
                }
            }
        }else if(prices.size()>1){
            return "价格类型重复";
        }else {
            if(priceMapper.updatePrice(price)){
                return "成功";
            }else {
                return "失败";
            }
        }
    }



    @Override
    public String deletePrice(Integer id) {
        if (taximeterMapper.findTaximeterByPriceid(id).size() > 0 || paramFileMapper.findParamFileByPriceid(id).size() > 0) {
            return "该价格类型已被引用";
        } else {
            if (priceMapper.deletePrice(id)) {
                return "成功";
            } else {
                return "失败";
            }
        }
    }
}
